import UIKit

let array: [Int] = [1,2,3,4,5]

for i in 0..<array.count {
  //遍历数组到当前项时,查询下一项,必须确保在安全索引内,不能越界 i必须<array.count - 1
  if i < array.count - 1 {
    let currentItem = array[i]
    let nextItem = array[i+1]
    print("current: \(currentItem) next: \(nextItem)")
  }
}




class KKModel {
  var value: Int
  
  init(value: Int) {
    self.value = value
  }
}

class CustomTableViewCell: UITableViewCell {
  
  var models: [KKModel] = [] {
    didSet {
      calculateValues()
    }
  }
  
  private func calculateValues() {
    for i in 0..<models.count {
      let currentItem = models[i]
      var nextItem: KKModel?
      
      // 检查是否有下一项
      if i + 1 < models.count {
        nextItem = models[i + 1]
      }
      
      // 对当前项和下一项进行计算
      if let nextItem = nextItem {
        // 示例计算：将当前项和下一项的值相加
        let result = currentItem.value + nextItem.value
        print("Current Item: \(currentItem.value), Next Item: \(nextItem.value), Result: \(result)")
      } else {
        // 当前项是最后一项，没有下一项
        print("Current Item: \(currentItem.value), Next Item: None")
      }
    }
  }
}
